#!/usr/bin/perl

do '../web-lib.pl';

&init_config();

#accounts_user_list(&user_fullname_list, &user_disable_list)
#return @user_list
sub accounts_user_list()
{
	my @user_list = ();
	my $pt_user_fullname_list = $_[0];
	my $pt_user_disable_list = $_[1];
	my @local_user = ();
	my @line_array = ();
	my $i = 0;

	my $openerror = open(LOCALUSER, $config{"local_user_file"});

	if(!$openerror)
	{
		@user_list = ();
		return @user_list;
	}

	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		chomp($local_user[$i]);
		@line_array = split(":", $local_user[$i]);
		if($line_array[0])
		{
			@user_list[$i] = $line_array[0];
			$pt_user_fullname_list->{$line_array[0]} = $line_array[1];
			$pt_user_disable_list->{$line_array[0]} = $line_array[2];
		}
		$i++;
	}
	@user_list = sort @user_list;
	return @user_list;
}

#get_user_grouplist($username)
#return @grouplist
sub get_user_grouplist()
{
	my @grouplist_rt=();
	my @groups_sys=();
	my @groups_local=();
	my @groups_tmp=();
	my @line_array=();
	my $username=$_[0];
	my $line;
	my $grouploc;
	my $groupsys;
	my $i;

	open(LOCALGROUP, $config{"local_group_file"});
	@groups_local = <LOCALGROUP>;
	close(LOCALGROUP);
	foreach $line (@groups_local)
	{
		chomp($line);
	}

	open(SYSGROUP, $config{"sys_group_file"});

	@groups_sys = <SYSGROUP>;
	close(SYSGROUP);
	foreach $line (@groups_sys)
	{
		chomp($line);
	}

	$i = 0;
	foreach $groupsys (@groups_sys)
	{
		@line_array = split(":", $groupsys);
		if($line_array[3] =~ /$username/)
		{
			$groups_tmp[$i] = $line_array[0];
			$i++;
		}
	}

	$line = join(",", @groups_tmp);

	foreach $group_loc (@groups_local)
	{
		if($line =~ /$group_loc/)
		{
			@grouplist_rt = ($group_loc, @grouplist_rt);
		}
	}
	sort(@grouplist_rt);

	return @grouplist_rt;
}

#get groupid just as users group id
#return the groupid of users which group name is "users"
sub get_users_groupid()
{
	my @groups_sys = ();
	my $i;
	my @line;
	open(SYSGROUP, $config{"sys_group_file"});
	@groups_sys = <SYSGROUP>;
	close(SYSGROUP);

	$i = 0;
	while($i < @groups_sys)
	{
		chomp($groups_sys[$i]);
		@line = split(":", $groups_sys[$i]);
		if($line[0] eq "users")
		{
			return $line[2];
		}
		$i++;
	}
}

#get_new_userid(void)
#get a new user id and id is not same as other users
#return id;
sub get_new_userid()
{
	my $id;
	my $i;
	my $base_uid = 20001;
	my %used_userid;
	my $max = 0;
	my @line;
	my @user_sys = ();
	my @localidfile;

	@user_sys = `getent passwd 2>&1`;

#	open(SYSUSER, $config{'sys_user_file'});
#	@user_sys = <SYSUSER>;
#	close(SYSUSER);

	open(LOCALID, $config{'localidfile'});
	@localidfile = <LOCALID>;
	close(LOCALID);

	for($i = 0; $i < @localidfile; $i++)
	{
		$localidfile[$i] =~ s/#.*$//;
		chomp($localidfile[$i]);
		if($localidfile[$i] =~ /uid/i)
		{
			@line = split(/\s+/, $localidfile[$i]);
			if($line[1] > $base_uid)
			{
				$base_uid = $line[1] + 1;
			}
			last;
		}
	}

	$i = 0;
	#print "@user_sys";
	while($i < @user_sys)
	{
		chomp($user_sys[$i]);
		@line = split(":", $user_sys[$i]);
		$used_userid{"$line[2]"} = "1";
		$i++;
	}

	$id = $base_uid;
	while($used_userid{"$id"})
	{
		$id++;
	}
	return $id;
}

#get_new_groupid(void)
#get a new group id and id is not same as other group
#return id;
sub get_new_groupid()
{
	my $id;
	my $i;
	my $base_gid = 20001;
	my %used_groupid;
	my $max = 0;
	my @line;
	my @group_sys = ();
	my @localidfile;

	@group_sys = `getent group 2>&1`;

	open(LOCALID, $config{'localidfile'});
	@localidfile = <LOCALID>;
	close(LOCALID);

	for($i = 0; $i < @localidfile; $i++)
	{
		$localidfile[$i] =~ s/#.*$//;
		chomp($localidfile[$i]);
		if($localidfile[$i] =~ /gid/i)
		{
			@line = split(/\s+/, $localidfile[$i]);
			if($line[1] > $base_gid)
			{
				$base_gid = $line[1] + 1;
			}
			last;
		}
	}

	$i = 0;

	while($i < @group_sys)
	{
		chomp($group_sys[$i]);
		@line = split(":", $group_sys[$i]);
		$used_groupid{"$line[2]"} = "1";
		$i++;
	}

	$id = $base_gid;
	while($used_groupid{"$id"})
	{
		$id++;
	}
	return $id;
}

#get_localid(void)
#return @localid;
sub get_localid()
{
	my @localid, $i;
	open(LOCALID, "$config{'localidfile'}");
	@localid = <LOCALID>;
	close(LOCALID);

#	for ($i = 0; $i < @localid; $i++)
#	{
#		chomp($localid[$i]);
#	}
	return @localid;
}

#$set_localid($uid, $gid)
#return void
sub set_localid()
{
	my @localid, $i, $uid, $gid;
	$uid = @_[0];
	$gid = @_[1];

	@localid = &get_localid();
	for ($i = 0; $i < @localid; $i++)
	{
		if(($localid[$i] =~ /uid/i) && ($uid > 0))
		{
			$localid[$i] = join(" ", "uid", $uid, "\n");
		}
		if(($localid[$i] =~ /gid/i) && ($gid > 0))
		{
			$localid[$i] = join(" ", "gid", $gid, "\n");
		}
	}

	open(LOCALID, ">$config{'localidfile'}");
	print LOCALID @localid;
	close(LOCALID);
}


#find_uid_by_name($username)
#return uid, -1 --> not found
sub find_uid_by_name()
{
	my ($uid, @user_sys, $i, @line, $username);
	$username = $_[0];

	open(SYSUSER, $config{'sys_user_file'});
	@user_sys = <SYSUSER>;
	close(SYSUSER);

	$i = 0;
	while($i < @user_sys)
	{
		chomp($user_sys[$i]);
		@line = split(":", $user_sys[$i]);
		if($line[0] eq $username)
		{
			return $line[2];
			last;
		}
		$i++;
	}
	if($i >= @user_sys)
	{
		return "-1";
	}
}

#create_user(%detail)
sub create_user()
{
	my ($users_detail, $i, $rt_msg, @glist);
	my @line, $pt;
	$users_detail = $_[0];

	#lockuserfile

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});
	&foreign_call("useradmin", "create_user", $users_detail);


	$users_detail->{'passmode'} = "3";

	#create users in other mofules
	&foreign_call("useradmin", "other_modules", "useradmin_create_user", $users_detail);

	@glist = ();
	foreach $i (@{$users_detail->{'glist'}})
	{
		push @glist, $i;
	}

	&add_user_into_group(\@glist, $users_detail->{'user'});

	open(LOCALUSER, ">>$config{'local_user_file'}");
	#push SYSGROUP, $users_detail->{"user"}."\n";
	print LOCALUSER $users_detail->{"user"}.":".$users_detail->{"real"}.":".$users_detail->{"disabled"}."\n";
	close(LOCALUSER);

	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");

	#excute dir should be absulutly?
	if($users_detail->{"disabled"} eq "yes")
	{
		system("/usr/bin/passwd -l $users_detail->{'user'} 2>/dev/null");
	}
	elsif($users_detail->{"disabled"} eq "no")
	{
		system("/usr/bin/passwd -u $users_detail->{'user'} 2>/dev/null");
	}
}

#find_in_array(\@array, $str)
#return an integer num -1 -> notfind   rt_find  -> address in array
sub find_in_array()
{
	my ($lref, $str, $rt_find, $i, $line);
	$lref = $_[0];
	$str = $_[1];
	$rt_find = -1;

	$i = 0;

	while($i < @$lref)
	{
		$line = $lref->[$i];
		if($line eq $str)
		{
			$rt_find = $i;
			return $rt_find;
		}
		$i++;
	}
	return $rt_find;
}

#get_user_passwd($username)
#return shadow passwd
sub get_user_passwd()
{
	my ($rt_pass, @sys_shadow, $i, @line, $username);

	@sys_shadow = ();
	@line = ();
	$i = 0;
	$username = @_[0];
	$rt_pass = "";

	open(SYS_SHADOW, $config{"sys_shadow"});
	@sys_shadow = <SYS_SHADOW>;
	close(SYS_SHADOW);

	$i = 0;
	while($i < @sys_shadow)
	{
		@line = split(":", $sys_shadow[$i]);
		if($line[0] eq $username)
		{
			$rt_pass = $line[1];
			last;
		}
		$i++;
	}

	return $rt_pass;
}

#get_user_detail(&detail, $username)
sub get_user_detail()
{
	my ($uid, $shell, $pass, $num, $max, $change, $expire, $min, $real, $real, $gid, $inactive);
	my ($user, $home, $line, $sline, $warn);
	my ($i, $username, @sys_passwd, @sys_shadow, $pt_detail, @linearray);

	$pt_detail = @_[0];
	$username = @_[1];

	open(SYS_PASSWD, $config{"sys_user_file"});
	@sys_passwd = <SYS_PASSWD>;
	close(SYS_PASSWD);

	open(SYS_SHADOW, $config{"sys_shadow"});
	@sys_shadow = <SYS_SHADOW>;
	close(SYS_SHADOW);

	$i = 0;
	while($i < @sys_passwd)
	{
		chomp($sys_passwd[$i]);
		@linearray = split(":", $sys_passwd[$i]);
		if($linearray[0] eq $username)
		{
			$num = $i;
			$line = $i;
			last;
		}
		$i++;
	}

	$i = 0;
	while($i < @sys_shadow)
	{
		chomp($sys_shadow[$i]);
		@linearray = split(":", $sys_shadow[$i]);
		if($linearray[0] eq $username)
		{
			$sline = $i;
			last;
		}
		$i++;
	}

	@linearray = split(":", $sys_passwd[$line]);
	$uid = $linearray[2];
	$shell = $linearray[6];
	$home = $linearray[5];
	$real = $linearray[4];
	$gid = $linearray[3];
	$user = $linearray[0];

	@linearray = split(":", $sys_shadow[$line]);
	$pass = $linearray[1];
	$change = $linearray[2];
	$min = $linearray[3];
	$max = $linearray[4];
	$expire = "";
	$inactive = "";
	$warn = "";

	$pt_detail->{"shell"} = $shell;
	$pt_detail->{"pass"} = $pass;
	$pt_detail->{"num"} = $num;
	$pt_detail->{"max"} = $max;
	$pt_detail->{"change"} = $change;
	$pt_detail->{"expire"} = $expire;
	$pt_detail->{"min"} = $min;
	$pt_detail->{"real"} = $real;
	$pt_detail->{"gid"} = $gid;
	$pt_detail->{"inactive"} = $inactive;
	$pt_detail->{"user"} = $username;
	$pt_detail->{"line"} = $line;
	$pt_detail->{"home"} = $home;
	$pt_detail->{"sline"} = $sline;
	$pt_detail->{"warn"} = $warn;
	$pt_detail->{"uid"} = $uid;
}

#modify_user(\%user, \%olduser)
sub modify_user()
{
	my ($pt_user_detail, $pt_olduser_detail);
	my (@sys_group, @sys_pssswd, @sys_shadow, @local_user, @local_group, @glist);
	my ($i, @line);

	$pt_user_detail = @_[0];
	$pt_olduser_detail = @_[1];

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});
	&foreign_call("useradmin", "modify_user", $pt_olduser_detail, $pt_user_detail);

	&foreign_call("useradmin", "other_modules", "useradmin_modify_user", $pt_user_detail, $pt_olduser_detail);

	&remove_user_from_group($pt_olduser_detail->{'user'});

	@glist = ();
	foreach $i (@{$pt_user_detail->{'glist'}})
	{
		push @glist, $i;
	}

	&add_user_into_group(\@glist, $pt_user_detail->{'user'});

	&modify_local_user($pt_olduser_detail, $pt_user_detail);



	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");

	if($pt_user_detail->{"disabled"} eq "yes")
	{
		system("/usr/bin/passwd -l $pt_user_detail->{'user'} 2>/dev/null");
	}
	elsif($pt_user_detail->{"disabled"} eq "no")
	{
		system("/usr/bin/passwd -u $pt_user_detail->{'user'} 2>/dev/null");
	}
}

#remove_user_from_group($username)
sub remove_user_from_group()
{
	my(@line, @lref_sys_file, @lineuser, @lref_gshadow);
	my $i, $j, $pt_glist, $username;

	$username = @_[0];

	open(SYSGROUP, $config{"sys_group_file"});
	@lref_sys_file = <SYSGROUP>;
	close(SYSGROUP);

	open(SYSGSHADOW, $config{"sys_gshadow_file"});
	@lref_gshadow = <SYSGSHADOW>;
	close(SYSGSHADOW);

	$i = 0;
	while($i < @lref_sys_file)
	{
		@line = split(":", $lref_sys_file[$i]);

		chomp @line[3];
		if($line[3])
		{
			@lineuser = split(",", $line[3]);
			$j = 0;
			while($j < @lineuser)
			{
				if($lineuser[$j] eq "$username")
				{
					splice(@lineuser, $j, 1);
					last;
				}
				$j++;
			}
			$line[3] = join(",", @lineuser);
			$lref_sys_file[$i] = join(":", @line);
			$lref_sys_file[$i] = $lref_sys_file[$i]."\n";
		}
		$i++;
	}

	$i = 0;
	while($i < @lref_gshadow)
	{
		@line = split(":", $lref_gshadow[$i]);

		chomp @line[3];
		if($line[3])
		{
			@lineuser = split(",", $line[3]);
			$j = 0;
			while($j < @lineuser)
			{
				if($lineuser[$j] eq "$username")
				{
					splice(@lineuser, $j, 1);
					last;
				}
				$j++;
			}
			$line[3] = join(",", @lineuser);
			$lref_gshadow[$i] = join(":", @line);
			$lref_gshadow[$i] = $lref_gshadow[$i]."\n";
		}
		$i++;
	}

	open(SYSGROUP, ">$config{'sys_group_file'}");
	print SYSGROUP @lref_sys_file;
	close(SYSGROUP);

	open(SYSGSHADOW, ">$config{'sys_gshadow_file'}");
	print SYSGSHADOW @lref_gshadow;
	close(SYSGSHADOW);
}

#add_user_from_group(\@glist, $username)
sub add_user_into_group()
{
	my(@line, @lref_sys_file, @lref_gshadow);
	my $i, $pt_glist, $username;

	$pt_glist = @_[0];
	$username = @_[1];

	open(SYSGROUP, $config{"sys_group_file"});
	@lref_sys_file = <SYSGROUP>;
	close(SYSGROUP);

	open(SYSGSHADOW, $config{"sys_gshadow_file"});
	@lref_gshadow = <SYSGSHADOW>;
	close(SYSGSHADOW);

	$i = 0;
	while($i < @lref_sys_file)
	{
		@line = split(":", $lref_sys_file[$i]);

		if(&find_in_array($pt_glist, $line[0]) >= 0)
		{
			my $tmparray;
			my @tmpusers;

			chomp($line[3]);

			if($line[3])
			{
				$line[3] = $line[3].",".$username;

			}
			else
			{
				$line[3] = $username;

			}
			$line[3] = $line[3]."\n";
			$lref_sys_file[$i] = join(":", @line);

		}
		$i++;
	}

	$i = 0;
	while($i < @lref_gshadow)
	{
		@line = split(":", $lref_gshadow[$i]);
	
		if(&find_in_array($pt_glist, $line[0]) >= 0)
		{
			my $tmparray;
			my @tmpusers;

			chomp($line[3]);

			if($line[3])
			{
				$line[3] = $line[3].",".$username;
			}
			else
			{
				$line[3] = $username;
			}
			$line[3] = $line[3]."\n";
			$lref_gshadow[$i] = join(":", @line);
		}
		$i++;
	}


	open(SYSGROUP, ">$config{'sys_group_file'}");
	print SYSGROUP @lref_sys_file;
	close(SYSGROUP);

	open(SYSGSHADOW, ">$config{'sys_gshadow_file'}");
	print SYSGSHADOW @lref_gshadow;
	close(SYSGSHADOW);
}

#modify local user name to same as passwd file change
#modify_local_user(\%olduser, \%newuser)
sub modify_local_user()
{
	my (@local_user, $i, @line, $olduser, $newuser);

	$olduser = @_[0];
	$newuser = @_[1];

	open(LOCALUSER, $config{"local_user_file"});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $olduser->{"user"})
		{
			$local_user[$i] = join(":", $newuser->{"user"}, $newuser->{"real"}, $newuser->{"disabled"});
			$local_user[$i] = $local_user[$i]."\n";
			last;
		}
		$i++;
	}

	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close(LOCALUSER);
}

#modify_local_group($oldname, $newname)
sub modify_local_group()
{
	my ($i, @group_local, $oldname, $newname, $line);

	$oldname = @_[0];
	$newname = @_[1];

	open(LOCALGROUP, $config{"local_group_file"});
	@group_local = <LOCALGROUP>;
	close(LOCALGROUP);

	$i = 0;
	while($i < @group_local)
	{
		$line = $group_local[$i];
		chomp($line);
		if($line eq $oldname)
		{
			$group_local[$i] = $newname."\n";
			last;
		}
		$i++;
	}

	open(LOCALGROUP, ">$config{'local_group_file'}");
	print LOCALGROUP @group_local;
	close(LOCALGROUP);
}

#delete_user($username)
sub delete_user()
{
	my (%detail, $username);
	$username = @_[0];

	system("/usr/bin/passwd -u $username 2>/dev/null");

	&get_user_detail(\%detail, $username);

	&foreign_require("useradmin", "user-lib.pl");
	&foreign_call("useradmin", "lock_user_files");
	&lock_file($config{"local_user_file"});

	#delete in other modules
	&foreign_call("useradmin", "other_modules", "useradmin_delete_user", \%detail);

	#delete in passwd and shadow
	&foreign_call("useradmin", "delete_user", \%detail);

	&remove_user_from_group($username);

	&delete_local_user($username);

	&unlock_file($config{"local_user_file"});
	&foreign_call("useradmin", "unlock_user_files");
}

#when delete a localuser for unix in localfile will change as unix
#delete_local_user($username)
sub delete_local_user()
{
	my (@local_user, $i, @line, $username);

	$username = @_[0];


	open(LOCALUSER, $config{"local_user_file"});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = @local_user - 1;
	while($i >= 0)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			splice(@local_user, $i, 1);
			last;
		}
		$i--;
	}

	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close(LOCALUSER);
}

#delete_local_group($groupname)
sub delete_local_group()
{
	my (@local_group, $i, $line, $groupname);

	$groupname = @_[0];

	open(LOCALGROUP, $config{"local_group_file"});
	@local_group = <LOCALGROUP>;
	close(LOCALGROUP);

	$i = @local_group;
	while($i > 0)
	{
		$line = $local_group[$i - 1];
		chomp($line);
		if($line eq $groupname)
		{
			splice(@local_group, $i - 1, 1);
			last;
		}
		$i--;
	}

	open(LOCALGROUP, ">$config{'local_group_file'}");
	print LOCALGROUP @local_group;
	close(LOCALGROUP);
}

#disable_user($username)
sub disable_user()
{
	my ($username, @local_user, $i, @line);

	$username = @_[0];

	open(LOCALUSER, $config{'local_user_file'});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			chomp($line[2]);
			system("/usr/bin/passwd -l $username 2>/dev/null");
			$line[2] = "yes"."\n";
			$local_user[$i] = join(":", @line);
			last;
		}
		$i++;
	}

	&lock_file($config{'local_user_file'});
	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close LOCALUSER;
	&unlock_file($config{'local_user_file'});
}

#enable_user($username)
sub enable_user()
{
	my($username, @local_user, $i, @line);

	$username = @_[0];

	open(LOCALUSER, $config{'local_user_file'});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		@line = split(":", $local_user[$i]);
		if($line[0] eq $username)
		{
			chomp($line[2]);
			system("/usr/bin/passwd -u $username 2>/dev/null");
			$line[2] = "no"."\n";
			$local_user[$i] = join(":", @line);
			last;
		}
		$i++;
	}

	&lock_file($config{'local_user_file'});
	open(LOCALUSER, ">$config{'local_user_file'}");
	print LOCALUSER @local_user;
	close LOCALUSER;
	&unlock_file($config{'local_user_file'});
}

#get_local_group_list(\%groupmember)
#return @grouplist
sub get_local_group_list()
{
	my (@grouplist, $pt_groupmember, $i, @sysgroup, @localgroup, @line, $addr);
	my (@umembers, @localuser, $j);

	@grouplist = ();
	$pt_groupmember = @_[0];

	open(SYSGROUP, "$config{'sys_group_file'}");
	@sysgroup = <SYSGROUP>;
	close(SYSGROUP);

	open(LOCALGROUP, "$config{'local_group_file'}");
	@localgroup = <LOCALGROUP>;
	close(LOCALGROUP);

 	open(LOCALUSER, "$config{'local_user_file'}");
	@localuser = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @localgroup)
	{
		chomp($localgroup[$i]);
		$i++;
	}

	$i = 0;
	while($i < @localuser)
	{
		chomp($localuser[$i]);
		@line = split(":", $localuser[$i]);
		$localuser[$i] = $line[0];
		$i++;
	}

	$i = 0;
	while($i < @sysgroup)
	{
		@line = split(":", $sysgroup[$i]);
		$addr = &find_in_array(\@localgroup, $line[0]);
		if($addr >= 0)
		{
			chomp($line[3]);
			if($line[3])
			{
				@umembers = split(",", $line[3]);
				$j = @umembers;
				while($j > 0)
				{
					if(&find_in_array(\@localuser, $umembers[$j - 1]) < 0)
					{
						splice(@umembers, $j - 1, 1);
					}
					$j--;
				}
				$pt_groupmember->{$line[0]} = join(",", @umembers);
			}
			else
			{
				$pt_groupmember->{$line[0]} = "";
			}
			push @grouplist, $line[0];
		}
		$i++;
	}

	@grouplist = sort @grouplist;
	return @grouplist;
}

#get_new_groupid(void)
#return $newgroupid
#sub get_new_groupid()
#{
#	my $newgid;
#	my $base_gid = 500;
#	my $i;
#	my %used_groupid;
#	my $max = 0;
#	my @line;
#	my @group_sys = ();
#	open(SYSGROUP, $config{'sys_group_file'});
#	@group_sys = <SYSGROUP>;
#	close(SYSGROUP);

#	$i = 0;

#	while($i < @group_sys)
#	{
#		chomp($group_sys[$i]);
#		@line = split(":", $group_sys[$i]);
#		$used_groupid{"$line[2]"} = "1";
#		$i++;
#	}
#
#	$newgid = $base_gid;
#	while($used_groupid{"$newgid"})
#	{
#		$newgid++;
#	}
#	return $newgid;
#}

#get_nolocal_members($groupname)
#return $notlocal_members
sub get_nolocal_members()
{
	my (@sys_group, $i, @local_user, @line, $groupname, $rt_members, @members, $j, @no_local_members);

	$groupname = @_[0];
	@no_local_members = ();

	open(SYSGROUP, $config{"sys_group_file"});
	@sys_group = <SYSGROUP>;
	close(SYSGROUP);

	open(LOCALUSER, $config{"local_user_file"});
	@local_user = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @local_user)
	{
		chomp($local_user[$i]);
		@line = split(":", $local_user[$i]);
		$local_user[$i] = $line[0];
		$i++;
	}

	$i = 0;
	while($i < @sys_group)
	{
		@line = split(":", $sys_group[$i]);
		if($line[0] eq $groupname)
		{
			chomp($line[3]);
			@members = split(",", $line[3]);
			$j = 0;
			while($j < @members)
			{
				if(&find_in_array(\@local_user, $members[$j]) < 0)
				{
					push @no_local_members, $members[$j];
				}
				$j++;
			}
			last;
		}
		$i++;
	}

	$rt_members = join(",", @no_local_members);
	return $rt_members;
}

#find_gid_by_gname($groupname)
#return gid       -1 --> not found
sub find_gid_by_gname()
{
	my ($gid, @sys_group, $i, @line, $groupname);
	$groupname = $_[0];

	open(SYSGROUP, $config{'sys_group_file'});
	@sys_group = <SYSGROUP>;
	close(SYSGROUP);

	$i = 0;
	while($i < @sys_group)
	{
		chomp($sys_group[$i]);
		@line = split(":", $sys_group[$i]);
		if($line[0] eq $groupname)
		{
			return $line[2];
			last;
		}
		$i++;
	}
	if($i >= @sys_group)
	{
		return "-1";
	}
}

#local_group_exist($groupname)
#return addr  -1 --> not found
sub local_group_exist()
{
	my (@localgroup, $i, @line, $groupname);

	$groupname = @_[0];

	open(LOCALGROUP, $config{'local_group_file'});
	@localgroup = <LOCALGROUP>;
	close(LOCALGROUP);

	$i = 0;
	while($i < @localgroup)
	{
		chomp($localgroup[$i]);
		if($localgroup[$i] eq $groupname)
		{
			return "$i";
			last;
		}
		$i++;
	}
	if($i >= @localgroup)
	{
		return "-1";
	}
}

#local_user_exist($username)
sub local_user_exist()
{
	my ($i, @localuser, @line, $username);
	$username = @_[0];

	open(LOCALUSER, $config{"local_user_file"});
	@localuser = <LOCALUSER>;
	close(LOCALUSER);

	$i = 0;
	while($i < @localuser)
	{
		chomp($localuser[$i]);
		@line = split(":", $localuser[$i]);
		if($line[0] eq $username)
		{
			return $i;
			last;
		}
		$i++;
	}
	if($i >= @localgroup)
	{
		return "-1";
	}
}

#get_group_detail(\%groupdetail)
#return gid        -1 --> not found
sub get_group_detail()
{
	my ($pt_group, @sys_group, @line, $i, $groupname, @sys_gshadow, $rt_gid);

	$pt_group = @_[0];
	$groupname = $pt_group->{"group"};

	open(SYSGROUP, $config{"sys_group_file"});
	@sys_group = <SYSGROUP>;
	close(SYSGROUP);

	open(SYSGSHADOW, $config{"sys_gshadow_file"});
	@sys_gshadow = <SYSGSHADOW>;
	close(SYSGSHADOW);

	$i = 0;
	while($i < @sys_group)
	{
		chomp($sys_group[$i]);
		@line = split(":", $sys_group[$i]);
		if($line[0] eq $groupname)
		{
			$pt_group->{"gid"} = $line[2];
			$pt_group->{"members"} = $line[3];
			$pt_group->{"pass"} = "";
			$pt_group->{"line"} = $i;
			$pt_group->{"num"} = $i;
			$rt_gid = $line[2];
			last;
		}
		$i++;
	}

	if($i >= @sys_group)
	{
		$rt_gid = -1;
		return $rt_gid;
	}

	$i = 0;
	while($i < @sys_gshadow)
	{
		chomp($sys_gshadow[$i]);
		@line = split(":", $sys_gshadow[$i]);
		if($line[0] eq $groupname)
		{
			$pt_group->{"sline"} = $i;
		}
		$i++;
	}
	return $rt_gid;
}

#create_group(\%group)
#group-->pass          passwd
#group-->group         groupname
#group-->gid	       groupid
#group-->members       unixmembers
sub create_group()
{
	my ($i, @line, @group_local, $pt_group);

	$pt_group = @_[0];

	if(&find_gid_by_gname($pt_group->{"group"}) < 0)
	{
		&foreign_require("useradmin", "user-lib.pl");
		&foreign_call("useradmin", "lock_user_files");
		&lock_file($config{"local_group_file"});

		&protect_gshadow();
		&foreign_call("useradmin", "create_group", $pt_group);
		&unprotect_gshadow();
		

		open(LOCALGROUP, ">>$config{'local_group_file'}");
		print LOCALGROUP $pt_group->{"group"}."\n";
		close(LOCALGROUP);

		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd", "add_group", $pt_group);

		&sync_http_group();

		&set_localid(-1, $pt_group->{"gid"});

		&unlock_file($config{"local_group_file"});
		&foreign_call("useradmin", "unlock_user_files");
	}
}

#modify_group(#old, #detail)
#old-->{"pass", "line", "sline", "num", "group", "members", "gid"} --> {...}
#detail-->{"pass", "group", "gid", "members"} --> {...}
sub modify_group()
{
	my ($i, @line, @group_local, $pt_group, $pt_old, $error_msg);

	$pt_old = @_[0];
	$pt_group = @_[1];
	$error_msg = "";

	unless($pt_old->{"group"} eq $pt_group->{"group"})
	{
		if(&find_gid_by_gname($pt_group->{"group"}) >= 0)
		{
			$error_msg = "groupname exist!";
		}
	}

	unless($error_msg)
	{
		&foreign_require("useradmin", "user-lib.pl");
		&foreign_call("useradmin", "lock_user_files");
		&lock_file($config{"local_group_file"});

		&protect_gshadow();
		&foreign_call("useradmin", "modify_group", $pt_old, $pt_group);
		&unprotect_gshadow();


		&modify_local_group($pt_old->{"group"}, $pt_group->{"group"});

		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd", "mod_group", $pt_old, $pt_group);

		&sync_http_group();

		&unlock_file($config{"local_group_file"});
		&foreign_call("useradmin", "unlock_user_files");
	}
}

sub delete_group()
{
	my ($pt_group, $groupname, $error_msg);

	$pt_group = @_[0];
	$groupname = $pt_group->{"group"};
	$error_msg = "";

	unless($groupname)
	{
		$error_msg = "gname empty";
	}
	else
	{
		if(&local_group_exist($groupname) < 0)
		{
			$error_msg = "local group not exist";
		}
	}

	unless($error_msg)
	{

		&foreign_require("useradmin", "user-lib.pl");
		&foreign_call("useradmin", "lock_user_files");
		&lock_file($config{"local_group_file"});

		&protect_gshadow();
		&foreign_call("useradmin", "delete_group", $pt_group);
		&unprotect_gshadow();


		&delete_local_group($groupname);

		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd", "del_group", $groupname);

		&sync_http_group();

		&unlock_file($config{"local_group_file"});
		&foreign_call("useradmin", "unlock_user_files");
	}

}

#display_5_members($strmembers)
sub display_5_members()
{
	my ($strmembers, $i, @line);

	$strmembers = @_[0];

	@line = split(",", $strmembers);
	if(@line < 5)
	{
		return $strmembers;
	}
	return join(",", $line[0], $line[1], $line[2], $line[3]."...");
}

sub sync_http_group()
{
	my ($i, @http_group, @local_group, %lg, %group, @line, $members, $gpname);

	open(HTTPGROUP, "$config{'http_group_file'}");
	@http_group = <HTTPGROUP>;
	close(HTTPGROUP);

	open(LOCALGROUP, "$config{'local_group_file'}");
	@local_group = <LOCALGROUP>;
	close(LOCALGROUP);

	$i = 0;
	while($i < @local_group)
	{
		$local_group[$i] =~ s/#.*$//;
		chomp($local_group[$i]);
		if($local_group[$i])
		{
			$group{"group"} = $local_group[$i];
			&get_group_detail(\%group);
			$members = $group{"members"};
			@line = split(",", $members);
			if($members)
			{
				$lg{$local_group[$i]} = join(" ", @line);
			}
			else
			{
				$lg{$local_group[$i]} = "";
			}
		}
		$i++;
	}

	&lock_file("$config{'http_group_file'}");

	open(HTTPGROUP, ">$config{'http_group_file'}");
	foreach $gpname (keys %lg)
	{
		print HTTPGROUP join(":", $gpname, $lg{$gpname}), "\n";

	}
	close(HTTPGROUP);

	&unlock_file("$config{'http_group_file'}");
}

#sub check_simbol_uname($inputname)
#return $error_msg
sub check_simbol_uname()
{
	my ($inputname, $error_msg);

	$inputname = @_[0];
	$error_msg = "";

	if(($inputname =~ /[^a-zA-Z0-9]/) || ($inputname =~ /^\d+(.*)/))
	{
		$error_msg = $text{'error_msg_isnt_simbol'};
	}

	return $error_msg;
}

#protect shadow file  copy gshadow.bak to /tmp/ and touch gshadow.tch on /tmp/
sub protect_gshadow()
{
	my ($out);

	$out = &ez_system("cp", "-f", $config{"sys_gshadow_file"}, $config{'gshadowbak'});
	$out = &ez_system("touch", $config{'gshadowtch'});
	
}

#remove shadow.tch protect file
sub unprotect_gshadow()
{
	my ($out);

	if(-e $config{'gshadowtch'})
	{
		$out = &ez_system("rm", "-f", $config{'gshadowtch'});
	}
}

#remote_foreigncall for mdm module
#mdm_addgroup($groupname)
sub mdm_addgroup()
{
        my ($groupname, $gid, $error_msg, %group, $newgid, @line);
	
	$groupname = @_[0];

	$gid = &local_group_exist($groupname);
	
	if ($gid >= 0)
	{
	        $error_msg = $text{'error_msg_group_exist_local'};
		return $error_msg;
	}

	$gid = &find_gid_by_gname($groupname);

	unless ($gid < 0)
	{
	        $error_msg = $text{'error_msg_group_exist_sys'};
		return $error_msg;
	}

	$newgid = &get_new_groupid();
	@line = split(/\s+/, "");

	$group{"pass"} = "";
	$group{"group"} = $groupname;
	$group{"gid"} = $newgid;
	$group{"members"} = join(",", @line);

	&create_group(\%group);

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_delgroup($groupname)
sub mdm_delgroup()
{
        my ($groupname, $gid, $error_msg, %group);

	$groupname = @_[0];

	$gid = &local_group_exist($groupname);

	if ($gid < 0)
	{
	        $error_msg = $text{'error_msg_group_not_exist_local'};
		return $error_msg;
	}

	$group{"group"} = $groupname;
	&get_group_detail(\%group);

	&delete_group(\%group);

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_addmembers($groupname, \@members)
sub mdm_addmembers()
{
        my ($groupname, $ptmb, $k, $gid, %group, %ogroup, %addmb, @orgmb, $uid);

	$groupname = @_[0];
	$ptmb = @_[1];

	$gid = &local_group_exist($groupname);
	
	if($gid < 0)
	{
	        $error_msg = $text{'error_msg_group_not_exist_local'};
		return $error_msg;
	}
	
	$group{"group"} = $groupname;
	&get_group_detail(\%group);
	$ogroup{"group"} = $groupname;
	&get_group_detail(\%ogroup);

	
	foreach $k (@$ptmb)
	{
	        $addmb{$k} = "yes";
	}

	chomp($ogroup{'members'});
	@orgmb = split(",", $ogroup{'members'});

	foreach $k (@orgmb)
        {	    
	        if ($addmb{$k} eq "yes")
		{
		        $addmb{$k} = "no";
		}
	}

	foreach $k (keys %addmb)
	{
	        if($addmb{$k} eq "yes")
		{
		        $uid = &local_user_exist($k);
		        push @orgmb, $k if ($uid >= 0);
		}
	}

	$group{"members"} = join(",", @orgmb);

	&modify_group(\%ogroup, \%group);

	return "Success";
}

#remote_foreigncall for mdm module
#mdm_delmembers($groupname, \@members)
sub mdm_delmembers()
{
        my ($groupname, $ptmb, $k, $gid, %group, %ogroup, %delmb, @orgmb, $i, $uid);
	

	$groupname = @_[0];
	$ptmb = @_[1];

	$gid = &local_group_exist($groupname);
	
	if($gid < 0)
	{
	        $error_msg = $text{'error_msg_group_not_exist_local'};
		return $error_msg;
	}
	
	$group{"group"} = $groupname;
	&get_group_detail(\%group);
	$ogroup{"group"} = $groupname;
	&get_group_detail(\%ogroup);

	
	foreach $k (@$ptmb)
	{
	        $delmb{$k} = "yes";		
	}

	@orgmb = split(",", $ogroup{'members'});

	for($i = @orgmb - 1; $i >= 0; $i--)
        {
	        if ($delmb{$orgmb[$i]} eq "yes")
		{
		        $uid = &local_user_exist($k);
		        splice @orgmb, $i, 1 if (uid >= 0);
		}
	}

	$group{"members"} = join(",", @orgmb);

	&modify_group(\%ogroup, \%group);

	return "Success";
}

1;
